package com.xiaobai.sequentialControl;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

@Slf4j
public class AlternatingOperationByParkUnpark {
    static Thread Thread1;
    static Thread Thread2;
    static Thread Thread3;

    public static void main(String[] args) {
        WaitNotifyThread waitNotifyThread = new WaitNotifyThread(5);
        Thread1 = new Thread(() -> {
            waitNotifyThread.print("a", Thread2);
        });
        Thread2 = new Thread(() -> {
            waitNotifyThread.print("b", Thread3);
        });
        Thread3 = new Thread(() -> {
            waitNotifyThread.print("c", Thread1);
        });
        Thread1.start();
        Thread2.start();
        Thread3.start();
        LockSupport.unpark(Thread1);
    }
}

/*
我要输出的交替的abc
 */
@Slf4j
class WaitNotifyThread {

    public void print(String str, Thread next) {
        for (int i = 0; i < cyclesNumber; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }
    }

    //循环次数
    private int cyclesNumber;

    public WaitNotifyThread(int cyclesNumber) {

        this.cyclesNumber = cyclesNumber;
    }
}
